package Boundry;

import Boundry.GenericTools.ViewDataPanel;
import Controllers.GlobalData;
import Controllers.UsersManager;
import Driver.Driver;
import ICM_OCSF.Client;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.SwingWorker;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;

public class ReconnectPanel extends ViewDataPanel {

	public ReconnectPanel() {
		JOptionPane.showMessageDialog(this,"Connection Lost", "Error",JOptionPane.ERROR_MESSAGE);
		JButton btnReconnect = new JButton("");
		btnReconnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SwingWorker worker = new SwingWorker<Void, Void>() {
					@Override
					 public Void doInBackground() 
					 {			
						Splash splash = new Splash();
						splash.setInfo("Connecting to Server");
						GlobalData.client = new Client(Driver.getIp(),Integer.parseInt(Driver.getPort()));
						try {
							GlobalData.client.openConnection();
							CloseSplash(splash);
							UsersManager.getInstance().login();	
						} catch (IOException e1) {
							JOptionPane.showMessageDialog(splash,"Connection Failure", "Error",JOptionPane.ERROR_MESSAGE);
							CloseSplash(splash);
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						//Driver.StartUp();
						return null;
					 }
					public void done(){
						
					}
				};
				worker.execute();
				
			}
			private void CloseSplash(Splash splash) 
			{
				splash.setVisible(false);
				splash.dispose();
			}
		});
		btnReconnect.setIcon(new ImageIcon(ReconnectPanel.class.getResource("/GUI/Image/10_device_access_network_wifi.png")));
		btnReconnect.setBounds(388, 146, 89, 69);
		mainPanel.add(btnReconnect);
		
		JLabel lblTryToConnect = new JLabel("Try to connect");
		lblTryToConnect.setBounds(398, 226, 89, 14);
		mainPanel.add(lblTryToConnect);
		// TODO Auto-generated constructor stub
	}
}
